<?php

namespace StarCloud\Library\Sdk;

use Framework\DiServices\HttpClient;
use Phalcon\Db\Column;
use Phalcon\Di;
use StarCloud\Api\Library\Exception;
use StarCloud\Library\ArraySort;
use StarCloud\Library\HttpCurl;
use StarCloud\Models\Doumvip\FanqieBook;
use StarCloud\Models\Doumvip\FanqieBookDetail;
use StarCloud\Models\Doumvip\LofterPost;
use StarCloud\Models\Doumvip\TiktokNew;

class Lofter
{
    use HttpCurl;

    /* @var $httpClient HttpClient */
    private $httpClient = null;

    public function __construct()
    {
        $this->httpClient = Di::getDefault()->get('httpClient');
        $this->cacheClass = Di::getDefault()->get('redis');
    }

    public function getHeader()
    {
        return [
          'Cookie' => 'usertrack=c+53cWVe2wKgU1qjNHzFAg==; hb_MA-BFD7-963BF6846668_source=yf101134.lofter.com; hb_MA-B4E8-3BEB9540671E_source=yf101134.lofter.com; _kol_token=F7zqZdwu/M5mmxinHhbKKtQju3CCiVIc4J+qzgMTng8='
        ];
    }

    public function getPostList()
    {
        // https://upup.lofter.com/api/kol/proword/list?limit=20&offset=0&type=post
        $url = 'https://upup.lofter.com/api/kol/content/query?limit=10000&offset=0&settlementType=CPA&type=post&hasImage=';
        $response = $this->httpClient->sendPostRequest($url, [], $this->getHeader());

        $json = json_decode($response->getBody()->getContents(), true);

        if (isset($json['code']) && $json['code'] == 200) {
            var_dump($json['data']['total']);
            foreach ($json['data']['list'] as $item) {
                LofterPost::uploadPost($item);
            }
        }

        return [];
    }

    public function feedback($id, $link)
    {
        $url = "https://upup.lofter.com/api/kol/proword/feedback";
        $body = [
            'id' => $id,
            'feedback' => json_encode(['sites' => ['快手'], 'urls' => ['kuaishou' => $link],])
        ];

        $header = $this->getHeader();
        $header['Content-Type'] = 'application/x-www-form-urlencoded';

        $response = $this->httpClient->sendPostRequestUrlEncode($url, $body, $header);

        $json = json_decode($response->getBody()->getContents(), true);


        var_dump($json);
        if (isset($json['code']) && $json['code'] == 200) {
            return true;
        }
        return false;
    }

    public function getPasswordList( $page, $size = 2000, $type = "post")
    {
        $offset = ($page - 1) * $size;
        $url = "https://upup.lofter.com/api/kol/proword/list?limit={$size}&offset={$offset}&type={$type}";
        $response = $this->httpClient->sendGetRequest($url, [], $this->getHeader());

        $json = json_decode($response->getBody()->getContents(), true);

        if (isset($json['data']['list']) && count($json['data']['list']) > 0) {
            return $json['data']['list'];
        }

        return [];
    }

    public function createKeyword($resourceId, $resourceType, $word)
    {
        $url = 'https://upup.lofter.com/api/kol/proword/new';

        $body = [
            "resourceId" => $resourceId,
            "resourceType" => $resourceType,
            "word" => $word,
        ];

        $response = $this->httpClient->sendPostRequestUrlEncode($url, $body, $this->getHeader());

        $json = json_decode($response->getBody()->getContents(), true);

        if (isset($json['code']) && $json['code'] == 200) {

            return true;
        }

        throw new \StarCloud\Library\Exception(9999, $json['msg']);
    }
}